using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace Finance.Web.Models
{
    public class DepositViewModel
    {
        public DepositViewModel() { }

        public DepositViewModel(Deposit deposit)
        {
            this.Code = deposit.Code;
            this.Name = deposit.Name;
            this.Info = deposit.Info;
            this.BankCode = deposit.BankCode;
            this.BankName = deposit.Bank != null ? deposit.Bank.Name : string.Empty;
            this.Percent = deposit.Percent;
            this.Capitalization = deposit.Capitalization;
            this.PaymentFrequesny = deposit.PaymentFrequesny;
            this.PeriodPayment = deposit.PeriodPayment;
            
            if (deposit.PercentAmountDepends != null)
                this.PercentAmountDepends = deposit.PercentAmountDepends.Select(i => new PercentAmountDependViewModel(i)).ToList();
           
            this.PercentAmountDepend = deposit.PercentAmountDepend;
            this.HasPenalties = deposit.HasPenalties;

            if (deposit.PercentPenalties != null)
                this.PercentPenalties = deposit.PercentPenalties.Select(i => new PercentPenaltyViewModel(i)).ToList();
        }

        public int Code { get; set; }
        public string Name { get; set; }
        public string Info { get; set; }
        public int? BankCode { get; set; }
        public string BankName { get; set; }

        public Nullable<double> Percent { get; set; }
        public Nullable<bool> Capitalization { get; set; }
        public Nullable<int> PaymentFrequesny { get; set; }
        public Nullable<int> PeriodPayment { get; set; }

        public ICollection<PercentAmountDependViewModel> PercentAmountDepends { get; set; }

        public ICollection<PercentPenaltyViewModel> PercentPenalties { get; set; }

        public bool? PercentAmountDepend { get; set; }

        public bool? HasPenalties { get; set; }
    }

    public partial class Deposit
    {
        public Deposit()
        {
            this.UserDeposit = new List<UserDeposit>();
            this.PercentAmountDepends = new List<PercentAmountDepend>();
            this.PercentPenalties = new List<PercentPenalty>();
        }

        public int Code { get; set; }
        [Required]
        public string Name { get; set; }
        public Nullable<int> BankCode { get; set; }
        public string Info { get; set; }
        public Nullable<double> Percent { get; set; }
        [Required]
        public Nullable<bool> Capitalization { get; set; }
        [Required]
        public Nullable<int> PaymentFrequesny { get; set; }
        [Required]
        public Nullable<int> PeriodPayment { get; set; }
        
        public virtual Bank Bank { get; set; }
        public virtual ICollection<UserDeposit> UserDeposit { get; set; }

        public virtual ICollection<PercentAmountDepend> PercentAmountDepends { get; set; }

        public Nullable<bool> PercentAmountDepend { get; set; }

        public Nullable<bool> HasPenalties { get; set; }

        public virtual ICollection<PercentPenalty> PercentPenalties { get; set; }
    }
}
